<template>
  <div class="sub-category">
    <div class="container">
      <!-- 面包屑 -->
      <SubBread />
      <!-- 筛选区 -->
      <SubFilter @filter-change="filterChange"/>

      <!-- 结果区域 -->
      <div class="goods-list">
        <!-- 排序 -->
        <subSort @sort-change="changeSort"/>

        <!-- 列表 -->
        <ul>
          <li v-for="goodsItems in goodsList" :key="goodsItems.id">
            <GoodsItem :goodsList="goodsItems" />
          </li>
        </ul>

        <XtxInfiniteLoading :loading="loading" :infinite="infinite" @infinite="getData"/>
      </div>
    </div>
  </div>
</template>

<script>
import subSort from './component/sub-sort.vue'
import SubBread from './component/sub-bread.vue'
import SubFilter from './component/sub-filter.vue'
import GoodsItem from './component/goods-item.vue'
import { ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import { findSubCategoryGoods } from '@/api/category'
export default {
  name: 'SubCategory',
  components: {
    SubBread,
    SubFilter,
    subSort,
    GoodsItem
  },
  setup () {
    const loading = ref(false) // 加载中动画
    const infinite = ref(false) // 加载完毕动画
    const goodsList = ref([])
    const route = useRoute()

    // 查询参数
    let reqParams = {
      page: 1,
      pageSize: 20
    }

    const getData = () => {
      loading.value = true // 开启加载模式

      reqParams.categoryId = route.params.id

      // 加载数据
      findSubCategoryGoods(reqParams).then(({ result }) => {
        // 判断是否有数据可加载
        if (result.items.length) {
          goodsList.value.push(...result.items)

          // 加载完之后继续加载下一页
          reqParams.page++
        } else {
          // 如果没有数据可加载
          infinite.value = true
        }
        loading.value = false // 关闭加载中开关
      })
    }

    // 切换二级分类的时候 更换商品id
    watch(() => route.params.id, newVal => {
      // 当侦听到路由地址的id发生改变时，将数据置为原来
      if (newVal && route.path === `/category/sub/${newVal}`) {
        goodsList.value = []
        reqParams = {
          page: 1, // 查询页数
          pageSize: 20 // 每次加载的数量
        }
        // 将加载完毕开关关闭
        infinite.value = false
      }
    }, { immediate: true })

    // 监听筛选区改变 如果有的话重新将接口参数置为原值
    const filterChange = (filterParams) => {
      reqParams = { ...reqParams, ...filterParams }
      reqParams.page = 1
      goodsList.value = []
      infinite.value = false
    }

    // 监听排序改变 如果有的话重新将接口参数置为原值
    const changeSort = (sortParams) => {
      reqParams = { ...reqParams, ...sortParams }
      reqParams.page = 1
      goodsList.value = []
      infinite.value = false
    }

    return {
      loading,
      infinite,
      getData,
      goodsList,
      changeSort,
      filterChange
    }
  }
}
</script>

<style lang="less" scoped>
.goods-list {
  background: #fff;
  padding: 0 25px;
  margin-top: 25px;

  ul {
    display: flex;
    flex-wrap: wrap;
    padding: 0 5px;
    li {
      margin-right: 20px;
      margin-bottom: 20px;
      &:nth-child(5n) {
        margin-right: 0;
      }
    }
  }
}
</style>
